#include "resourcetreeview.h"
#include <pthread.h>
#include <stdio.h>
#include <QX11EmbedWidget>
#include <QVBoxLayout>
#include <QApplication>
#include <QClipboard>
#include <QVariant>
#include <QPalette>
#include <QColor>
#include <QFont>
#include <gdk/gdkx.h>
#include <gtk/gtkstyle.h>
#include <gtk/gtkwidget.h>
#include <gtk/gtkmain.h>

#include "ccfresourceeditor.h"

static pthread_key_t *envKey = 0;

CCF_MAIN_BEGIN("ccfresourceeditor", "ide.ccf.ccproject.pages.embedded", "{3d3ddadb-26c6-4574-b3a1-a6fad1bfa46e}", "{131f2e61-bb08-4804-a626-8d233261ee30}")
    CCF_CLASS(ResourceTreeView, "resourcetreeview.h", "resourcetreeview.cpp")
CCF_MAIN_END()


JNIEXPORT jlong JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_createControl(JNIEnv *env, jclass that, jlong parent, jlong socketWin)
{
    if (!QApplication::instance())
    {
        Display *xdisp = XOpenDisplay(0);
        (void)new QApplication(xdisp);

        #if QT_VERSION >= 0x040400
        qApp->setAttribute(Qt::AA_NativeWindows);
        #endif

        qApp->clipboard()->setProperty("useEventLoopWhenWaiting", QVariant(true));

        GtkStyle *style = gtk_widget_get_style((GtkWidget *)parent);
        GdkColor col = style->bg[GTK_STATE_NORMAL];
        QPalette qtpal(QColor(col.red>>8, col.green>>8, col.blue>>8));

        col = style->text[GTK_STATE_SELECTED];
        qtpal.setColor(QPalette::Normal, QPalette::HighlightedText, QColor(col.red>>8, col.green>>8, col.blue>>8));

        col = style->base[GTK_STATE_SELECTED];
        qtpal.setColor(QPalette::Normal, QPalette::Highlight, QColor(col.red>>8, col.green>>8, col.blue>>8));

        QApplication::setPalette(qtpal);
        QFont::insertSubstitution("Sans", "Sans Serif");
    }

    if (!envKey)
    {
        envKey = new pthread_key_t;
        pthread_key_create(envKey, NULL);
    }

    pthread_setspecific(*envKey, env);
    
    ResourceTreeView *obj = new ResourceTreeView();
    QX11EmbedWidget *client = new QX11EmbedWidget();
    
    obj->setParent(client);
    
    QVBoxLayout *vblayout = new QVBoxLayout(client);
    vblayout->setMargin(0);
    
    ResourceTreeViewListener *lstnr = new ResourceTreeViewListener(obj, envKey, that);
    QObject::connect(obj, SIGNAL(dirtyChanged()), lstnr, SLOT(slot_dirtyChanged()));
    vblayout->addWidget(obj);

    client->embedInto(socketWin);
    client->show();
    return (jlong)obj;
}

JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_computeSize(JNIEnv *env, jclass that, jlong handle, jintArray result)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;
    jint *nresult = NULL;

    nresult = env->GetIntArrayElements(result, NULL);
    
    nresult[0] = obj->parentWidget()->sizeHint().width();
    nresult[1] = obj->parentWidget()->sizeHint().height();
    
    env->ReleaseIntArrayElements(result, nresult, 0);
    
    Q_UNUSED(env);
    Q_UNUSED(that);
}

JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_resizeControl(JNIEnv *env, jclass that, jlong handle, jint x, jint y, jint width, jint height)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;

    obj->parentWidget()->resize(width, height);
    obj->parentWidget()->move(x, y);
    
    Q_UNUSED(env);
    Q_UNUSED(that);
}

JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_disposeControl(JNIEnv *env, jclass that, jlong handle)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;
    QWidget *parentW = obj->topLevelWidget();

    delete parentW;

    Q_UNUSED(env);
    Q_UNUSED(that);
}

JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_setFont(JNIEnv *env, jclass that, jlong handle, jstring jni_family, jint size)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;
    const char *utf_family = env->GetStringUTFChars(jni_family, 0);
    QString family = QString::fromUtf8(utf_family);

    env->ReleaseStringUTFChars(jni_family, utf_family);
    
    obj->setFont(QFont(family, size));
    
    Q_UNUSED(that);
}

JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_load(JNIEnv *env, jclass that, jlong handle, jstring jni_file)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;

    Q_UNUSED(that);

    const char *utf_file = env->GetStringUTFChars(jni_file, 0);
    QString file = QString::fromUtf8(utf_file);

    env->ReleaseStringUTFChars(jni_file, utf_file);

    bool res = obj->load(file);

    Q_UNUSED(env);

    return (jboolean)res;
}

JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_save(JNIEnv *env, jclass that, jlong handle)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;

    Q_UNUSED(that);

    bool res = obj->save();

    Q_UNUSED(env);

    return (jboolean)res;
}

JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_isDirty(JNIEnv *env, jclass that, jlong handle)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;

    Q_UNUSED(that);

    bool res = obj->isDirty();

    Q_UNUSED(env);

    return (jboolean)res;
}

JNIEXPORT jstring JNICALL Java_ide_ccf_ccproject_pages_embedded_ResourceTreeView_contents(JNIEnv *env, jclass that, jlong handle)
{
    ResourceTreeView *obj = (ResourceTreeView*)handle;

    Q_UNUSED(that);

    QString res = obj->contents();

    return env->NewStringUTF(res.toUtf8());
}

